<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>
        let datas = {
            "dataPack": {
                "rows": [
                    {
                        "FID": "2308FFAC-0BD1-2781-743C-586752CDA2BB",
                        "FARRIVALSTATE": "未到货",
                        "FCREATEDATE": "2020-12-24 17:25:56.0",
                        "FNOTE": "11",
                        "FENTERPRISEDESC": "福建康佰家大药房连锁有限公司",
                        "FSUPPLIER": "云南白药集团股份有限公司",
                        "FNUMBER": "CGDD-201224-0004",
                        "FMONEY": 5880,
                        "caigoudingdan.bizstore|BizEntity_mfHGTV": [
                            {
                                "FID": "4010FB9C-E1CC-DD15-AEF3-69879F9E0E92",
                                "FBARCODE": "11002850736",
                                "FRATEMONEY": 405.6,
                                "FPRICE": 33.8,
                                "FSORT": 0,
                                "FRATE": 0.12,
                                "FCREATEDATE": "2020-12-24 17:25:56.0",
                                "FNOTE": "11",
                                "FENTERPRISEDESC": "福建康佰家大药房连锁有限公司",
                                "FUNIT": "盒",
                                "FDESC": "阿司匹林肠溶片",
                                "FNUMBER": "CGDD-201224-0004",
                                "FMONEY": 3380,
                                "FGOODSNUMBER": "18248154576",
                                "FENTERPRISEID": "KANGBAIJIA",
                                "FBRAND": "三九999",
                                "FSAUDIT": 1,
                                "FUSERID": "admin",
                                "FAMOUNT": 100
                            },
                            {
                                "FID": "44498B42-5E7C-9D04-43B5-C5988ADF18B9",
                                "FBARCODE": "456464545",
                                "FRATEMONEY": 300,
                                "FPRICE": 25,
                                "FSORT": 0,
                                "FRATE": 0.12,
                                "FCREATEDATE": "2020-12-24 17:25:56.0",
                                "FNOTE": "11",
                                "FENTERPRISEDESC": "福建康佰家大药房连锁有限公司",
                                "FUNIT": "盒",
                                "FDESC": "龟黄补酒",
                                "FNUMBER": "CGDD-201224-0004",
                                "FMONEY": 2500,
                                "FGOODSNUMBER": "123456464566",
                                "FENTERPRISEID": "KANGBAIJIA",
                                "FBRAND": "云南白药",
                                "FSAUDIT": 1,
                                "FUSERID": "admin",
                                "FAMOUNT": 100,
                                "caigoudingdan.bizstore|BizEntity_msfad": [
                                    {
                                        "FID": "4010FB9C-E1CC-DD15-AEF3-69879F9E0E92",
                                        "FBARCODE": "11002850736",
                                        "FRATEMONEY": 405.6,
                                        "FPRICE": 33.8,
                                        "FSORT": 0,
                                        "FRATE": 0.12,
                                        "FCREATEDATE": "2020-12-24 17:25:56.0",
                                        "FNOTE": "11",
                                        "FENTERPRISEDESC": "福建康佰家大药房连锁有限公司",
                                        "FUNIT": "盒",
                                        "FDESC": "阿司匹林肠溶片",
                                        "FNUMBER": "CGDD-201224-0004",
                                        "FMONEY": 3380,
                                        "FGOODSNUMBER": "18248154576",
                                        "FENTERPRISEID": "KANGBAIJIA",
                                        "FBRAND": "三九999",
                                        "FSAUDIT": 1,
                                        "FUSERID": "admin",
                                        "FAMOUNT": 100
                                    },
                                    {
                                        "FID": "44498B42-5E7C-9D04-43B5-C5988ADF18B9",
                                        "FBARCODE": "456464545",
                                        "FRATEMONEY": 300,
                                        "FPRICE": 25,
                                        "FSORT": 0,
                                        "FRATE": 0.12,
                                        "FCREATEDATE": "2020-12-24 17:25:56.0",
                                        "FNOTE": "11",
                                        "FENTERPRISEDESC": "福建康佰家大药房连锁有限公司",
                                        "FUNIT": "盒",
                                        "FDESC": "龟黄补酒",
                                        "FNUMBER": "CGDD-201224-0004",
                                        "FMONEY": 2500,
                                        "FGOODSNUMBER": "123456464566",
                                        "FENTERPRISEID": "KANGBAIJIA",
                                        "FBRAND": "云南白药",
                                        "FSAUDIT": 1,
                                        "FUSERID": "admin",
                                        "FAMOUNT": 100
                                    }
                                ]
                            }
                        ],
                        "FENTERPRISEID": "KANGBAIJIA",
                        "FSUPPLIERID": "DA1385EB-ED5B-D799-5323-C822577D1FAF",
                        "FAUDIT": 1,
                        "FUSERID": "admin"
                    }
                ]
            },
            "status": "200",
            "outParameter": {
                "factRecordSize": -1,
                "pageSize": 10,
                "pageNum": 1
            }
        }
        let source = {
            "sourcedept": {
                "targetEntity": "dept",
                "returnFields": {
                    "desc": "FENTERPRISEDESC",
                    "field13": "FNOTE"
                }
            },
            "caigoudingdan.bizstore|BizEntity_mfHGTV": {
                "targetEntity": "dept1",
                "returnFields": {
                    "desc": "FDESC"
                }
            },
            "caigoudingdan.bizstore|BizEntity_msfad": {
                "targetEntity": "dept2",
                "returnFields": {
                    "desc": "FDESC"
                }
            }
        }

        function AddZcQuery(rows, sourceId) {
            debugger;
            for (var i = 0; i < rows.length; i++) {
                debugger;
                var row = rows[i];
                row.__row = {};
                var fieldPair = source[sourceId];
                var returnFields = fieldPair.returnFields;
                // var targetEntity = "dept1"
                for (var fn in row) {
                    debugger;
                    var targetField = null;
                    for (var field in returnFields) {
                        if (returnFields[field] == fn) {
                            targetField = field;
                            break;
                        }
                    }
                    if (typeof row[fn] != "object" && !targetField) {
                        continue;
                    }
                    if (targetField) {
                        row.__row[targetField] = row[fn];
                    }
                    if (typeof row[fn] == "object" && fn != "__row") {
                        debugger
                        row.__row[source[fn].targetEntity] = this.AddZcQuery(row[fn], fn);
                    }
                }
                rows[i] = row.__row;
            }
            return rows;
        }
        console.log(AddZcQuery(datas.dataPack.rows, "sourcedept"))

    </script>
</body>

</html>